<template>
	<el-container>
	
		
		<el-header>
			<div class="left-panel">
				<!-- <el-button type="primary" icon="el-icon-plus" ></el-button> -->
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="关键词" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="list.apiObj" row-key="id" stripe @selection-change="selectionChange">
				<el-table-column type="selection"  width="50"></el-table-column>
				<el-table-column label="区域名字" prop="name" width="150"></el-table-column>
				<el-table-column label="所在房间" prop="room.name" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="300">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row)">编辑</el-button>
							<el-button text type="primary" size="small" @click="table_del(scope.row, scope.$index)">删除</el-button>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save v-if="showEdit" ref="saveDialog" @success="handleSaveSuccess" @closed="showEdit=false" ></save>
</template>

<script>
	import scSelectFilter from '@/components/scSelectFilter'
	import save from './save.vue'

	export default {
		name: 'listTab',
		components: {
			scSelectFilter,
			save
		},
		data() {
			return {
				f_id: "0",
				floor: [], // 楼层信息
				list: {
					apiObj: this.$API.area.list
				},
				search: {
					keyword: ""
				},
				showEdit: false,
				showArea: false,
				selection: [] // 多项选择的框
			}
		},
		created() {
			this.getSelectData()
		},
		methods: {
			async getSelectData()
			{ 
				let res = await this.$API.floor.list.get();
				if(res.code == 200){
					this.floor = res.data.rows
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			table_edit(row) {
				this.showEdit = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			async table_del(row, index) {
				var reqData = row.id
				var res = await this.$API.area.delete.delete(reqData);
				if(res.code == 200){
					this.$refs.table.removeIndex(index)
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			async batch_del()
			{
				var confirmRes = await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
					type: 'warning',
					confirmButtonText: '删除',
					confirmButtonClass: 'el-button--danger'
				}).catch(() => {})

				if(!confirmRes){
					return false
				}
				var ids = this.selection.map(v => v.id)
				this.$refs.table.removeKeys(ids)
				var res = await this.$API.area.delete.delete(ids);
				if(res.code == 200){
					this.$message.success("删除成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				console.log(data, mode)
				//为了减少网络请求，直接变更表格内存数据
				// if(mode=='add'){
				// 	this.$refs.table.unshiftRow(data)
				// }else if(mode=='edit'){
				// 	this.$refs.table.updateKey(data)
				// }
				//当然也可以暴力的直接刷新表格
				this.$refs.table.refresh()
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection
			},
		}
	}
</script>

<style>
</style>
